#! /bin/sh -e

case "$1" in
	start)
		if [ ! -x "/usr/bin/gem" ]; then
			cd /tmp/rubygems
			ruby setup.rb
		fi
		if [ ! -x "/usr/bin/rails" ]; then
			gem install rails -y
		fi
		if [ ! -x "/usr/bin/mongrel_rails" ]; then
			gem install mongrel -y
		fi
		cd /usr/share/RailsSampleApp
		mongrel_rails start -d
		exit 0
	;;
	
	stop)
		cd /usr/share/RailsSampleApp
		mongrel_rails stop
	;;
esac

exit 0

if [ -x "/etc/init.d/rails-sample" ]; then
	update-rc.d rails-sample defaults >/dev/null
	if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
		invoke-rc.d rails-sample start || exit $?
	else
		/etc/init.d/rails-sample start || exit $?
	fi
fi
